<?php

namespace app\api\controller;

use app\api\model\BottonUrlModel;
use app\BaseController;
use think\Exception;

class BottonUrl extends BaseController
{
    public function index()
    {
        try {
            //实例化模型
            $model = new BottonUrlModel();
            //  1: 查询所有的父级菜单
            $parentMenus = $model->field('id, name')
                ->where('p_id', 0)
                ->where('status', 1)
                ->select()
                ->toArray();
            // 3: 查询并添加子菜单到对应的父级菜单
            foreach ($parentMenus as &$parentMenu) {
                $childMenus = $model->field('id, name')
                    ->where('p_id', $parentMenu['id'])
                    ->where('status', 1)
                    ->select()
                    ->toArray();
                $parentMenu['children'] = $childMenus;
            }
            $this->success('获取成功', $parentMenus);
        } catch (Exception $e) {

        }
    }
}